package com.szy.bigevent.controller;

import com.szy.bigevent.pojo.Result;
import com.szy.bigevent.service.AliOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
public class UploadController {

    @Autowired
    private AliOssService ossService;

    @RequestMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file) {
        final String originalFilename = file.getOriginalFilename();
        //构造文件名
        String fileName = UUID.randomUUID().toString()
                + originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        String uploadUrl = null;
        try {
            InputStream stream = file.getInputStream();
            uploadUrl = ossService.upload(fileName, stream);
        } catch (IOException e) {
            return Result.error(e.getMessage());
        }
        return Result.success(uploadUrl,"图片上传成功");
    }
}
